package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderRoleDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderRole;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/*
 *@AUTHOR:LIUCHAO;
 *@DATE: 2020/9/13 10:14
 */
@Service(interfaceClass = OrderRoleService.class)
@Transactional
public class OrderRoleServiceImpl implements OrderRoleService {
    @Autowired
    private OrderRoleDao orderRoleDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Override
    public Result add(OrderRole orderRole) {
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderRole.getOrderDate());
        /*判断预约的日期是否已经设置预约人数,如果没有,不能进行预约*/
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        /*判断已预约人数不能大于可预约人数,预约已满,不能预约,返回false*/
        if (orderSetting.getNumber()<=orderSetting.getReservations()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        /*检查预约表中是否有这个号码,如果有,证明已经预约,不能重复预约*/
        Integer count =orderRoleDao.getCountByTelOrIdCard(orderRole);
        if (count!=0){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        /*做个定时任务,当时间过了 今天之后 将之前的预约信息删除掉,今天删除明天以及之前的预约人信息*/
        int orderAfterReservation = orderSetting.getReservations() + 1;
        OrderSetting newOrderSetting=new OrderSetting(orderRole.getOrderDate(),orderAfterReservation);
        orderSettingDao.addOrderReservation(newOrderSetting);/*预约成功*/
        System.out.println(orderRole);
        orderRoleDao.add(orderRole);/*添加预约人信息*/
        /*添加会员信息*/
        /*查看会员信息中是否已经存在*/
        Integer memberCount =memberDao.getCountByTelOrIdCard(orderRole.getTelephone(),orderRole.getIdCard());
        Member member=null;
        if (memberCount==0){
            member = new Member();
            member.setName(orderRole.getName());
            member.setPhoneNumber(orderRole.getTelephone());
            member.setIdCard(orderRole.getIdCard());
            member.setSex(orderRole.getSex().toString());
            member.setRegTime(orderRole.getOrderDate());
            memberDao.add(member);
        }


        return new Result(true,MessageConstant.ORDER_SUCCESS,orderRole.getId());
    }

    @Override
    public void deleteOrderRole(Date date) {
        orderRoleDao.deleteOrderRole(date);
    }

    @Override
    public Result orderSuccess(Integer id) {
           OrderRole orderRole=  orderRoleDao.findById(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,orderRole);
    }


}
